<template>
	<view class="container">
		<uni-list>
			<uni-list-item v-for="item,index in list" :key="index" :title="item.name"
				:thumb="item.icon" clickable="true" thumbSize="lg"
				:rightText="'￥'+item.price+'元'" :clickable="true" :showArrow="true"
				@click="toPage(item.id)"></uni-list-item>
		</uni-list>
	</view>
</template>




<script>
	export default {
		data() {
			return {
				"list": [

				]
			}
		},
		methods: {
			async loadfood() {
				const db = uniCloud.database()
				const collection = db.collection('foods')
				let res = await collection.get()
				if (res.errCode == 0) {
					this.list = res.result.data
				} else {
					console.log("获取数据错误");
				}
			},

			loadfoodcf() {
				uniCloud.callFunction({
					name: "hellocf",
					data: {},
					success: (res) => {
						console.log(res.result);
						this.list = res.result.data
					}
				})
			},
			// loadfoodyun() {
			// 	let foods = uniCloud.database().collection('foods')
			// 	foods.get().then(res => {
			// 		console.log(res.result);
			// 		this.list = res.result.data
			// 	})
			// },
			loadfood() {
				uni.request({
					url: 'http://localhost:8090/foodapp/food/list',
					method: 'POST',
					data: {},
					success: res => {
						if (res.data.success) {
							this.list = res.data.data
							uni.showToast({
								title: '成功',
								icon: 'none'
							});
						} else {
							this.list = []
							uni.showToast({
								title: '失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '失败',
							icon: 'none'
						});
					},
					complete: () => {

					}
				});
			},


			toPage(id) {
				uni.navigateTo({
					url: '../fooddetail/fooddetail?id=' + id
				})
			}
		},
		onShow() {
			console.log("onShow");
		},
		onHide() {
			console.log('onHide');
		},
		onUnload() {
			console.log('onUnload');
		},
		onPullDownRefresh() {
			this.loadfood()
			uni.stopPullDownRefresh();
		},
		onLoad() {
			this.loadfoodcf()
			uniCloud.callFunction({
				name: 'hellocf',

			}).then((res) => {
				console.log(res);
				this.list = res.result.data
			})
		}

	}
</script>

<style>

</style>